package com.ljb.baseadapter_listview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

/**
 * 通用的ViewHolder,从此以后再也不用造轮子了
 * Created by lijianbin on 2016/12/13.
 */
public class ViewHolder {
    private SparseArray<View> mViews;
    private View mConvertView;
    private int mPosition;
    private int mLayoutId;

    public ViewHolder(View convertView) {
        mViews = new SparseArray<>();
        mConvertView = convertView;
        mConvertView.setTag(this);
    }

    /**
     * 获取ViewHolder对象
     */
    public static ViewHolder getHolder(Context context, View convertView, ViewGroup parent, int layoutId, int position) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
            holder = new ViewHolder(convertView);
            holder.mLayoutId = layoutId;
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.mPosition = position;
        return holder;
    }

    /**
     * 根据viewId获取控件
     */
    public <T extends View> T getHolderView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    public View getConvertView() {
        return mConvertView;
    }

    public int getItemPosition() {
        return mPosition;
    }

    public int getLayoutId() {
        return mLayoutId;
    }

/**********************辅助方法,可根据需求自行扩展**********************/
    /**
     * 为TextView设置文本内容
     *
     * @param viewId
     * @param text
     * @return
     */
    public ViewHolder setText(int viewId, String text) {
        TextView view = getHolderView(viewId);
        view.setText(text);
        return this;
    }

    /**
     * 为TextView设置文本颜色
     *
     * @param viewId
     * @param textColor
     * @return
     */
    public ViewHolder setTextColor(int viewId, int textColor) {
        TextView view = getHolderView(viewId);
        view.setTextColor(textColor);
        return this;
    }

    /**
     * 为ImageView设置资源文件
     *
     * @param viewId
     * @param resId
     * @return
     */
    public ViewHolder setImageResource(int viewId, int resId) {
        ImageView view = getHolderView(viewId);
        view.setImageResource(resId);
        return this;
    }

    /**
     * 为ImageView设置Drawable
     *
     * @param viewId
     * @param drawable
     * @return
     */
    public ViewHolder setImageDrawable(int viewId, Drawable drawable) {
        ImageView view = getHolderView(viewId);
        view.setImageDrawable(drawable);
        return this;
    }

    /**
     * 为ImageView设置Bitmap
     *
     * @param viewId
     * @param bitmap
     * @return
     */
    public ViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
        ImageView view = getHolderView(viewId);
        view.setImageBitmap(bitmap);
        return this;
    }

    /**
     * View设置背景颜色
     *
     * @param viewId
     * @param color
     * @return
     */
    public ViewHolder setBackgroundColor(int viewId, int color) {
        View view = getHolderView(viewId);
        view.setBackgroundColor(color);
        return this;
    }

    /**
     * 为View设置背景资源
     *
     * @param viewId
     * @param backgroundRes
     * @return
     */
    public ViewHolder setBackgroundRes(int viewId, int backgroundRes) {
        View view = getHolderView(viewId);
        view.setBackgroundResource(backgroundRes);
        return this;
    }

    /**
     * 为View设置Visibility属性
     *
     * @param viewId
     * @param visibility
     * @return
     */
    public ViewHolder setVisibility(int viewId, int visibility) {
        View view = getHolderView(viewId);
        view.setVisibility(visibility);
        return this;
    }

    /**
     * 为ProgressBar设置进度
     *
     * @param viewId
     * @param progress
     * @return
     */
    public ViewHolder setProgress(int viewId, int progress) {
        ProgressBar view = getHolderView(viewId);
        view.setProgress(progress);
        return this;
    }

    /**
     * 为ProgressBar设置进度条的最大值
     *
     * @param viewId
     * @param max
     * @return
     */
    public ViewHolder setMax(int viewId, int max) {
        ProgressBar view = getHolderView(viewId);
        view.setMax(max);
        return this;
    }

    /**
     * 为RatingBar设置星星个数
     *
     * @param viewId
     * @param rating
     * @return
     */
    public ViewHolder setRating(int viewId, float rating) {
        RatingBar view = getHolderView(viewId);
        view.setRating(rating);
        return this;
    }

    /**
     * 为RatingBar设置星星的最多个数
     *
     * @param viewId
     * @param max
     * @return
     */
    public ViewHolder setRating(int viewId, int max) {
        RatingBar view = getHolderView(viewId);
        view.setMax(max);
        return this;
    }

    /**
     * 为View设置Tag标记
     *
     * @param viewId
     * @param tag
     * @return
     */
    public ViewHolder setTag(int viewId, Object tag) {
        View view = getHolderView(viewId);
        view.setTag(tag);
        return this;
    }

    /**
     * 为View设置带有key的tag标记
     *
     * @param viewId
     * @param key
     * @param tag
     * @return
     */
    public ViewHolder setTag(int viewId, int key, Object tag) {
        View view = getHolderView(viewId);
        view.setTag(key, tag);
        return this;
    }


    /**
     * 为可选择的Checkable控件设置选中状态
     *
     * @param viewId
     * @param checked
     * @return
     */
    public ViewHolder setChecked(int viewId, boolean checked) {
        Checkable view = getHolderView(viewId);
        view.setChecked(checked);
        return this;
    }

/************************* 关于事件的一些方法,可根据需求自行扩展***************************/

    /**
     * 为View设置点击事件
     *
     * @param viewId
     * @param listener
     * @return
     */
    public ViewHolder setOnClickListener(int viewId, View.OnClickListener listener) {
        View view = getHolderView(viewId);
        view.setOnClickListener(listener);
        return this;
    }

    /**
     * 为View设置触摸事件
     *
     * @param viewId
     * @param listener
     * @return
     */
    public ViewHolder setOnTouchListener(int viewId, View.OnTouchListener listener) {
        View view = getHolderView(viewId);
        view.setOnTouchListener(listener);
        return this;
    }

    /**
     * 为View设置长按事件
     *
     * @param viewId
     * @param listener
     * @return
     */
    public ViewHolder setOnLongClickListener(int viewId, View.OnLongClickListener listener) {
        View view = getHolderView(viewId);
        view.setOnLongClickListener(listener);
        return this;
    }

}
